<?php

namespace backend\controllers\auth;

use Exception;
use mdm\admin\models\Menu;
use mdm\admin\components\Helper;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use mdm\admin\models\searchs\Menu as MenuSearch;
/**
 * Class MenuController
 * @package backend\controllers\auth
 */
class MenuController extends \mdm\admin\controllers\MenuController
{

    /**
     * actionIndex
     * @return mixed|string
     */
    public function actionIndex()
    {
        $searchModel = new MenuSearch;
        $dataProvider = $searchModel->search(request()->getQueryParams());

        $dataProvider->pagination->pageSize = 0;

        $dataProvider->query->orderBy(['order'=>SORT_ASC]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }


    /**
     * actionUpdateOrder
     * @return Response
     */
    public function actionUpdateOrder()
    {
        try {
            $model = $this->findModel(unserialize(base64_decode(post('pk'))));
            $model->order = post('value');
            if(!$model->save()){
                throw new Exception(getModelErrorMsg($model));
            }
            Helper::invalidate();
            return $this->asJson([
                'code' => 1,
                'message' => '修改成功',
            ]);
        }catch (Exception $e){
            exceptionFormat($e);
            return $this->asJson([
                'code' => 0,
                'message' => $e->getMessage(),
            ]);
        }
    }

    /**
     * Creates a new Menu model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Menu;

        if ($model->load(post()) && $model->save()) {
            Helper::invalidate();
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * actionUpdate
     * @param int $id
     * @return mixed|string|Response
     * @throws NotFoundHttpException
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->menuParent) {
            $model->parent_name = $model->menuParent->name;
        }
        if ($model->load(post()) && $model->save()) {
            Helper::invalidate();
            return $this->redirect(['index']);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * actionToggleColumn
     * @return bool
     */
    public function actionToggleColumn()
    {
        response()->format = response()::FORMAT_JSON;

        if(!empty($model = Menu::findOne(post('id')))){
            $data = json_decode($model->data,true);
            $attribute = post('attribute');
            $data[$attribute] = $data[$attribute]??0;
            $data[$attribute] = (int)!$data[$attribute];
            $model->data = json_encode($data);
            $res = $model->save();
            if($res){
                Helper::invalidate();
            }
            return $res;
        }
    }
}
